﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Controls;
using System.Text;
namespace LightSwitchApplication
{
    public partial class SearchStudents
    {
        private const string ITEMS_CONTROL = "grid";
        private DataGrid _itemsControl = null;

        partial void SearchStudents_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            this.FindControl(ITEMS_CONTROL).ControlAvailable += DemoItems_ControlAvailable;
        }

        private void DemoItems_ControlAvailable(object send, ControlAvailableEventArgs e)
        {
            _itemsControl = e.Control as DataGrid;

            if (_itemsControl == null)
            {
                return;
            }

            //set the property on the grid that allows multiple selection 
            _itemsControl.SelectionMode = DataGridSelectionMode.Extended;
        }

        partial void showSelectedStudentsDetailReport_Execute()
        {
            if (_itemsControl == null) { return; }

            StringBuilder ids = new StringBuilder();

            foreach (Student item in _itemsControl.SelectedItems)
            {
                ids.Append(item.Id);
                ids.Append(", ");
            }

            Application.ShowStudentDetailReport(ids.ToString(0, ids.Length - 2));
        }

        partial void gridAddAndEditNew_Execute()
        {
            this.Application.ShowCreateNewStudent();
        }
    }
}
